package com.wgchao.mdmp.common.aal;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.text.SimpleDateFormat;

/**
 * @ClassName: AalData
 * @author: wgchao
 * @createTime: 2019/11/4 3:54 PM
 * @Description: TODO
 */
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class AalData {
    private String datastr;//上传的字符串
    private String start;//起始码 2
    private String ver;//版本号 2
    private String user;//用户号 1
    private String customer;//客户号 2
    private String modelCode;//机器代码 2
    private String deviceType;//机种码 2
    private String serialnumber;//系列号 9
    private ObjectData data;//数据 9
    private String year;//时间年 2
    private String month;//时间月 2
    private String day;//时间日 2
    private String hour;//时间时2
    private String minute;//时间分 2
    private String sum;//检校码 4
    private String imsi;// _ 15
    private String imei;// _ 15
    private String rest;//保留字符 10

    public static AalData getInitializtion(String dataStr){

        AalData ad = new AalData();
        ad.datastr = dataStr;
        int startLoc = 0;
        String childStr = null;
        ad.start = dataStr.substring(0, 2);
        startLoc += 2;
        childStr = dataStr.substring(startLoc);
        ad.ver = childStr.substring(0,2);
        startLoc += 2;
        childStr = dataStr.substring(startLoc);
        ad.user = childStr.substring(0,1);
        startLoc += 1;
        childStr = dataStr.substring(startLoc);
        ad.customer = childStr.substring(0,2);
        startLoc += 2;
        childStr = dataStr.substring(startLoc);
        ad.modelCode = childStr.substring(0,2);
        startLoc += 2;
        childStr = dataStr.substring(startLoc);
        ad.deviceType = childStr.substring(0,2);
        startLoc += 2;
        childStr = dataStr.substring(startLoc);
        ad.serialnumber = childStr.substring(0,9);
        startLoc += 9;
        childStr = dataStr.substring(startLoc);
        //TODO 数据部分
        if(ad.deviceType.equals("01")){//血压计
            ad.data = GXYData.getInitializtion(childStr.substring(0,9));
        }else if(ad.deviceType.equals("02")){//血糖仪
            ad.data = XTYData.getInitializtion(childStr.substring(0,9));
        }
        startLoc += 9;
        childStr = dataStr.substring(startLoc);
        ad.year = childStr.substring(0,2);
        startLoc += 2;
        childStr = dataStr.substring(startLoc);
        ad.month = childStr.substring(0,2);
        startLoc += 2;
        childStr = dataStr.substring(startLoc);
        ad.day = childStr.substring(0,2);
        startLoc += 2;
        childStr = dataStr.substring(startLoc);
        ad.hour = childStr.substring(0,2);
        startLoc += 2;
        childStr = dataStr.substring(startLoc);
        ad.minute = childStr.substring(0,2);
        startLoc += 2;
        childStr = dataStr.substring(startLoc);
        ad.sum = childStr.substring(0,4);
        startLoc += 4;
        childStr = dataStr.substring(startLoc);
        childStr = childStr.replaceFirst("_", "");//去除连接符
        startLoc += 1;
        ad.imsi = childStr.substring(0,15);
        startLoc += 15;
        childStr = dataStr.substring(startLoc);
        childStr = childStr.replaceFirst("_", "");//去除连接符
        startLoc += 1;
        ad.imei = childStr.substring(0,15);
        startLoc += 15;
        childStr = dataStr.substring(startLoc);
        ad.rest = childStr.substring(0,10);
        return ad;
    }

    //重写输出方法
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("上传数据串:"+this.datastr+"\n");
        sb.append("start 起始码 2:"+this.start+"\n");
        sb.append("ver 版本号 2:"+this.ver+"\n");
        sb.append("user 用户号 1:"+this.user+"\n");
        sb.append("customer 客户号 2:"+this.customer+"\n");
        sb.append("modelCode 机器代码 2:"+this.modelCode+"\n");
        sb.append("deviceType 机种码 2:"+this.deviceType+"\n");
        sb.append("serialnumber 系列号 9:"+this.serialnumber+"\n");
        sb.append(this.data.toString());
        sb.append("year 时间年 2:"+this.year+"\n");
        sb.append("month 时间月 2:"+this.month+"\n");
        sb.append("day 时间日 2:"+this.day+"\n");
        sb.append("hour 时间时2:"+this.hour+"\n");
        sb.append("minute 时间分 2:"+this.minute+"\n");
        sb.append("sum 检校码 4:"+this.sum+"\n");
        sb.append("imsi  _ 15:"+this.imsi+"\n");
        sb.append("imei  _ 15:"+this.imei+"\n");
        sb.append("rest 保留字符 10:"+this.rest+"\n");
        return sb.toString();
    }


}
